home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.07 Jul 93 / Bedrock Header Files / Support Includes / BRVMChun.h < prev    next >
Encoding:
Text File  |  1993-04-19  |  7.2 KB  |  329 lines  |  [TEXT/MPS ]

  1. // BedrockHeap.h 
  2. // Copyright © 1985-1992 by Apple Computer, Inc.  All rights fReserved.
  3.  
  4. #ifndef BRVMCHUN_H
  5. #define BRVMCHUN_H
  6.  
  7. #ifndef BRVMBEST_H
  8. #include "BRVMBest.h"
  9. #endif
  10.  
  11.  
  12. //----------------------------------------------------------------------------------------
  13. // Forward class declarations
  14. //----------------------------------------------------------------------------------------
  15.  
  16. class BR_CChunk;
  17. class BR_CChunkyBlock;
  18. class BR_CHeap;
  19. class BR_CChunkyBlockStack;
  20.  
  21.  
  22. //========================================================================================
  23. // STRUCT BR_SChunkyBlockHeader
  24. //========================================================================================
  25.  
  26. struct BR_SChunkyBlockHeader
  27. {
  28.     unsigned int fSizeIndex:4;
  29.     unsigned int fBlockIndex:4;
  30.     int fBlockType:4;
  31.     unsigned int fMagicNumber:4;
  32. };
  33.  
  34.  
  35. //========================================================================================
  36. // CLASS BR_CChunk
  37. //========================================================================================
  38.  
  39. struct BR_SChunkHeader
  40. {
  41.     unsigned short fBlockBusyBits;
  42. };
  43.  
  44.  
  45. class BR_CChunk
  46. {
  47. public:
  48.     BR_CChunk(short blocksPerChunk,
  49.               unsigned int sizeIndex,
  50.               BR_MemoryBlockSize blockSize);
  51.  
  52.     ~BR_CChunk();
  53.  
  54.     inline void* operator new(size_t,
  55.                        void* ptr);
  56.     inline void* operator new(size_t);
  57.     inline void operator delete(void*);
  58.  
  59.     BR_CChunkyBlock* GetBlock(unsigned int blkIndex,
  60.                               BR_MemoryBlockSize blkSize);
  61.     unsigned int GetSizeIndex();
  62.  
  63.     BR_Boolean IsBlockBusy(unsigned int whichBlock);
  64.     BR_Boolean IsBusy();
  65.  
  66.     void SetBlockBusy(unsigned int whichBlock,
  67.                       BR_Boolean busy);
  68.  
  69. private:
  70.     BR_SChunkHeader fHeader;
  71. };
  72.  
  73. //----------------------------------------------------------------------------------------
  74. // INLINES BR_CChunk
  75. //----------------------------------------------------------------------------------------
  76.  
  77. inline BR_CChunk::~BR_CChunk()
  78. {
  79. }
  80.  
  81. inline void* BR_CChunk::operator new(size_t,
  82.                    void* ptr)
  83. {
  84.     return ptr;
  85. }
  86.  
  87.  
  88. inline void* BR_CChunk::operator new(size_t)
  89. {
  90.     return NULL;
  91. }
  92.  
  93.  
  94. inline void BR_CChunk::operator delete(void*)
  95. {
  96. }
  97.  
  98. inline BR_CChunkyBlock* BR_CChunk::GetBlock(unsigned int blkIndex,
  99.                                             BR_MemoryBlockSize blkSize)
  100. {
  101.     BR_CChunkyBlock * blk = (BR_CChunkyBlock *)((BR_PointerDifference)this + sizeof(BR_SChunkHeader) + blkIndex * blkSize);
  102.     return blk;
  103. }
  104.  
  105.  
  106. inline unsigned int BR_CChunk::GetSizeIndex()
  107. {
  108.     BR_SChunkyBlockHeader * header = (BR_SChunkyBlockHeader *)((BR_PointerDifference)this + sizeof(BR_SChunkHeader));
  109.     return header->fSizeIndex;
  110. }
  111.  
  112.  
  113. inline BR_Boolean BR_CChunk::IsBusy()
  114. {
  115.     return fHeader.fBlockBusyBits != 0;
  116. }
  117.  
  118.  
  119. inline void BR_CChunk::SetBlockBusy(unsigned int whichBlock,
  120.                                     BR_Boolean busy)
  121. {
  122.     unsigned short mask = 0x0001;
  123.  
  124.     for (short i = 0; i < whichBlock; i++)
  125.         mask = mask << 1;
  126.  
  127.     if (busy)
  128.         fHeader.fBlockBusyBits |= mask;
  129.     else
  130.         fHeader.fBlockBusyBits &= ~mask;
  131. }
  132.  
  133.  
  134. //========================================================================================
  135. // CLASS BR_CChunkyBlock
  136. //========================================================================================
  137.  
  138. class BR_CChunkyBlock
  139. {
  140. public:
  141.     enum
  142.     {
  143.         kBusyOverhead = 2, 
  144.         kChunkyBlockType = BR_CBestFitBlock::kBestFitBlockType + 1, 
  145.         kMagicNumber = 0xA
  146.     };
  147.  
  148.  
  149.     void* operator new(size_t,
  150.                        void* ptr);
  151.     void* operator new(size_t);
  152.     inline void operator delete(void*);
  153.  
  154.     BR_CChunkyBlock();
  155.     BR_CChunkyBlock(unsigned int sizeIndex,
  156.                     unsigned int blockIndex);
  157.     ~BR_CChunkyBlock();
  158.  
  159.     BR_CChunk* GetChunk(BR_MemoryBlockSize blkSize);
  160.     BR_CChunkyBlock* GetNext();
  161.     unsigned int GetSizeIndex();
  162.  
  163.     BR_Boolean IsBusy(BR_MemoryBlockSize blockSize);
  164.  
  165.     void SetBusy(BR_MemoryBlockSize blockSize,
  166.                  BR_Boolean busy);
  167.     void SetNext(BR_CChunkyBlock* blk);
  168.  
  169. private:
  170.     // Fields present in both free and busy blocks.
  171.  
  172.     BR_SChunkyBlockHeader fBusyHeader;
  173.  
  174.     // Fields present in only free blocks.
  175.  
  176.     BR_CChunkyBlock* fNext;
  177. };
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // INLINES BR_CChunkyBlock
  181. //----------------------------------------------------------------------------------------
  182.  
  183. inline void* BR_CChunkyBlock::operator new(size_t,
  184.                    void* ptr)
  185. {
  186.     return ptr;
  187. }
  188.  
  189.  
  190. inline void* BR_CChunkyBlock::operator new(size_t)
  191. {
  192.     return NULL;
  193. }
  194.  
  195.  
  196. inline void BR_CChunkyBlock::operator delete(void*)
  197. {
  198. }
  199.  
  200. inline BR_CChunkyBlock::BR_CChunkyBlock()
  201. {
  202.     fBusyHeader.fBlockType = kChunkyBlockType;
  203.     fBusyHeader.fMagicNumber = kMagicNumber;
  204.     fNext = NULL;
  205. }
  206.  
  207.  
  208. inline BR_CChunkyBlock::~BR_CChunkyBlock()
  209. {
  210. }
  211.  
  212.  
  213. inline BR_CChunkyBlock::BR_CChunkyBlock(unsigned int sizeIndex,
  214.                                         unsigned int blockIndex)
  215. {
  216.     fBusyHeader.fSizeIndex = sizeIndex;
  217.     fBusyHeader.fBlockIndex = blockIndex;
  218.     fBusyHeader.fBlockType = kChunkyBlockType;
  219.     fBusyHeader.fMagicNumber = kMagicNumber;
  220.     fNext = NULL;
  221. }
  222.  
  223.  
  224. inline BR_CChunk* BR_CChunkyBlock::GetChunk(BR_MemoryBlockSize blkSize)
  225. {
  226.     BR_CChunk * chk = (BR_CChunk *)((BR_PointerDifference)this - sizeof(BR_SChunkHeader) - blkSize * fBusyHeader.fBlockIndex);
  227.     return chk;
  228. }
  229.  
  230.  
  231. inline BR_CChunkyBlock* BR_CChunkyBlock::GetNext()
  232. {
  233.     return fNext;
  234. }
  235.  
  236.  
  237. inline unsigned int BR_CChunkyBlock::GetSizeIndex()
  238. {
  239.     return fBusyHeader.fSizeIndex;
  240. }
  241.  
  242.  
  243. inline BR_Boolean BR_CChunkyBlock::IsBusy(BR_MemoryBlockSize blockSize)
  244. {
  245.     return this->GetChunk(blockSize)->IsBlockBusy(fBusyHeader.fBlockIndex);
  246. }
  247.  
  248.  
  249. inline void BR_CChunkyBlock::SetBusy(BR_MemoryBlockSize blockSize,
  250.                                      BR_Boolean busy)
  251. {
  252.     this->GetChunk(blockSize)->SetBlockBusy(fBusyHeader.fBlockIndex, busy);
  253. }
  254.  
  255.  
  256. inline void BR_CChunkyBlock::SetNext(BR_CChunkyBlock* blk)
  257. {
  258.     fNext = blk;
  259. }
  260.  
  261.  
  262. //========================================================================================
  263. // CLASS BR_CChunkyBlockStack
  264. //========================================================================================
  265.  
  266. class BR_CChunkyBlockStack
  267. {
  268. public:
  269.     BR_CChunkyBlockStack();
  270.     ~BR_CChunkyBlockStack();
  271.  
  272.     BR_CChunkyBlock* Pop();
  273.     void Push(BR_CChunkyBlock* blk);
  274.     void RemoveRange(BR_PointerDifference begAddr,
  275.                      BR_PointerDifference endAddr);
  276.     BR_CChunkyBlock* Top();
  277.  
  278. private:
  279.     BR_CChunkyBlock fHead;
  280. };
  281.  
  282.  
  283. //========================================================================================
  284. // CLASS BR_CHeap
  285. //========================================================================================
  286.  
  287. class BR_CHeap : public BR_CBestFitHeap
  288. {
  289. private:
  290.     static const BR_MemoryBlockSize kDefaultBlockSizes[];
  291.  
  292. public:
  293.     enum
  294.     {
  295.         kMaxNumberOfBlockSizes = 16, kDefaultBlocksPerChunk = 4
  296.     };
  297.  
  298.  
  299.     BR_CHeap(BR_MemoryBlockSize initialSize,
  300.              BR_MemoryBlockSize incrementSize = 0,
  301.              short blocksPerChunk = kDefaultBlocksPerChunk,
  302.              const BR_MemoryBlockSize* blockSizes = kDefaultBlockSizes);
  303.  
  304.     virtual void* DoAllocate(BR_MemoryBlockSize size,
  305.                              BR_MemoryBlockSize& allocatedSize);
  306.     virtual BR_MemoryBlockSize DoBlockSize(const void* block) const;
  307.     virtual void DoFree(void*);
  308.     virtual BR_Boolean DoIsValidBlock(void* blk) const;
  309.     virtual void DoReset();
  310.  
  311.     virtual~ BR_CHeap();
  312.  
  313. protected:
  314.     void* AllocateBlock(unsigned int sizeIndex);
  315.     void CreateNewChunk(unsigned int sizeIndex);
  316.     void FreeBlock(BR_CChunkyBlock* blk);
  317.     unsigned int SizeIndex(BR_MemoryBlockSize size);
  318.  
  319. private:
  320.     short fNumberOfBlockSizes;
  321.     const BR_MemoryBlockSize* fBlockSizes;
  322.     BR_CChunkyBlockStack fFreeLists[kMaxNumberOfBlockSizes];
  323.     short fBlocksPerChunk;
  324. };
  325.  
  326.  
  327.  
  328. #endif
  329.